package cn.chinasyq.photoquan.user.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.bean.UserInfo;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.master.util.utils.ResourcesUtils;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.handler.ResponseHandler.OnFailedListener;
import cn.master.volley.models.response.handler.ResponseHandler.OnSucceedListener;

/**
 * Created by 一搏 on 2015/11/23.
 */
public class LoginActivity extends Activity implements View.OnClickListener,OnSucceedListener,OnFailedListener{
    private TextView tv_hint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tv_hint = (TextView) findViewById(R.id.tv_tips);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_close:
                onBackPressed();
                break;
            case R.id.btn_register:
                Intent i = new Intent(getApplicationContext(),RegisterActivity.class);
               startActivity(i);
                break;
            case R.id.btn_login:
                login(((EditText) findViewById(R.id.et_phone)).getText().toString(), ((EditText) findViewById(R.id.et_pass)).getText().toString());
                break;
            case R.id.forgot:
                startActivity(new Intent(getApplicationContext(),ResetPassActivity.class));
                break;
            default:
                break;
        }
    }
    private final String TAG_LOGIN = "login";
    private void login(String phone, String pass) {
        ResponseHandler regHandler = new ResponseHandler(TAG_LOGIN);
        regHandler.setOnFailedListener(this);
        regHandler.setOnSucceedListener(this);
        UserMode.login(phone, pass, regHandler);
    }
    private void setHint(String str) {
        if (str==null){
            str=("登录失败");
        }
        tv_hint.setText(str);
        tv_hint.setVisibility(View.VISIBLE);
    }
    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode,String msg) {
        tv_hint.setVisibility(View.INVISIBLE);
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    setHint(ResourcesUtils.getString(getApplicationContext(), resultCode + ""));
                    break;
                case NETWORK_ERROR:
                    setHint(getString(R.string.servererrortips));
                    break;
            }
        }else {
            setHint(msg);
        }
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        tv_hint.setText("");
         if (TAG_LOGIN.equals(tag)){
            hideSoftKeyboard();
            setResult(RESULT_OK, getIntent());
             UserMode.save(getApplicationContext(),(UserInfo) data);
            finish();
        }
    }
    private void hideSoftKeyboard() {
        if (getCurrentFocus() != null
                && getCurrentFocus().getWindowToken() != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
